﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the audio.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="gid">The group ID.</param>
        /// <param name="aids">The audio IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        /// <added_by>Alexey Serafimov</added_by>
        public long Audio_Get(long? uid, long? gid, long?[] aids, ApiCallback<Audio[]> callback)
        {
            List<Parameter> lpar = new List<Parameter>();
            if (uid != null) lpar.Add(new Parameter("uid", uid.ToString()));
            if (gid != null) lpar.Add(new Parameter("gid", gid.ToString()));
            if (aids != null)
            {
                string aidsString = "";
                foreach (int i in aids)
                    aidsString += i.ToString() + ",";
                aidsString = aidsString.Remove(aidsString.Length - 1);

                lpar.Add(new Parameter("aids", aidsString));
            }

            return this.SendRequest("audio.get", callback, lpar.ToArray());
        }

        /// <summary>
        /// Gets audios by ID.
        /// </summary>
        /// <param name="audios">The audios ID in format userID_audioid.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Audio.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Audio_GetByID(string audios, ApiCallback<Audio[]> callback)
        {
            List<Parameter> lpar = new List<Parameter>();
            if (audios != null) lpar.Add(new Parameter("audios", audios.ToString()));

            return this.SendRequest("audio.getById", callback, lpar.ToArray());
        }

        /// <summary>
        /// Get the lyrics of audio.
        /// </summary>
        /// <param name="lyrics_id">The lyrics ID fetched from audio.get, audio.getById or audio.search.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Lyrics.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Audio_GetLyrics(long? lyrics_id, ApiCallback<Lyrics[]> callback)
        {
            List<Parameter> lpar = new List<Parameter>();
            if (lyrics_id != null) lpar.Add(new Parameter("lyrics_id", lyrics_id.ToString()));

            return this.SendRequest("audio.getLyrics", callback, lpar.ToArray());
        }


        /// <summary>
        /// Get the upload server for audio.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        /// <added_by>Alexey Serafimov</added_by>
        public long Audio_GetUploadServer(ApiCallback<AudioUploadServer> callback)
        {
            return this.SendRequest("audio.getUploadServer", callback);
        }

        /// <summary>
        /// Saves the audio.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="server">The server name.</param>
        /// <param name="audio">The audio parameter.</param>
        /// <param name="hash">The hash.</param>
        /// <param name="artist">The artist name.</param>
        /// <param name="title">The title of the audio.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        /// <added_by>Alexey Serafimov</added_by>
        public long Audio_Save(long app_id, string sig, string v, string server, string audio, string hash, string artist, string title, ApiCallback<Audio[]> callback)
        {
            List<Parameter> lpar = new List<Parameter>();
            lpar.Add(new Parameter("app_id", app_id.ToString()));
            lpar.Add(new Parameter("sig", sig.ToString()));
            lpar.Add(new Parameter("v", v));
            lpar.Add(new Parameter("server", server));
            lpar.Add(new Parameter("audio", audio));
            lpar.Add(new Parameter("hash", hash));

            if (artist != null) lpar.Add(new Parameter("artist", artist.ToString()));
            if (title != null) lpar.Add(new Parameter("title", title.ToString()));

            return this.SendRequest("audio.save", callback, lpar.ToArray());
        }

        /// <summary>
        /// Searches the audio.
        /// </summary>
        /// <param name="q">The search query.</param>
        /// <param name="sort">The sortink key. 1 by duration, 0 by adding date.</param>
        /// <param name="lyrics">The lyrics.</param>
        /// <param name="count">The count of returned items.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        /// <added_by>Alexey Serafimov</added_by>
        public long Audio_Search(string q, ApiCallback<Audio[]> callback, bool? sort = null, bool? lyrics = null, int? count = null, int? offset = null)
        {
            List<Parameter> lpar = new List<Parameter>();
            lpar.Add(new Parameter("q", q));
            if (sort != null) lpar.Add(new Parameter("sort", Convert.ToInt16(sort).ToString()));
            if (lyrics != null) lpar.Add(new Parameter("lyrics", Convert.ToInt16(lyrics).ToString()));
            if (count != null) lpar.Add(new Parameter("count", count.ToString()));
            if (offset != null) lpar.Add(new Parameter("offset", offset.ToString()));

            CallbackJsonPreprocess_Handler h = delegate(string s)
            {
                if (s.StartsWith("{\"error"))
                    return s;

                int start = s.IndexOf("[") + 1;
                int len = s.IndexOf(",") - start + 1;

                return s.Remove(start, len);
            };

            return this.SendRequest("audio.search", new CallbackData() { Callback = callback, CallbackJsonPreprocess = h }, lpar.ToArray());
        }
    }
}
